<html xmlns="http://www.w3.org/1999/xhtml"><head><style type="text/css">
dt {
  font-style: italic;
  margin-top: 15px;
  margin-bottom: 3px;
  margin-left: 0px;
  border-bottom: 1px dotted black;
}
dd {
  margin-left: 10px;
}
table {
  border-collapse:collapse;
  border: 1px solid black;
  margin-top: 7px;
}
th {
  border: 1px solid black;
  padding: 3 7 3 7;
}
td {
  border: 1px solid black;
  padding: 3 7 3 7;
}
</style></head><body><p><a name="invokespecial"></a><b>invokespecial</b> :
  invoke method belonging to a specific class : <a href="opcodes.html">index</a> : <a href="http://asm.objectweb.org/asm40/javadoc/user/org/objectweb/asm/MethodVisitor.html#visitMethodInsn(int,%20java.lang.String,%20java.lang.String,%20java.lang.String)">visitMethodInsn()</a></p><dl><dt>Description</dt><dd>invokespecial is used in certain special cases to invoke a
method Specifically, invokespecial is used to invoke:<br></br>the instance initialization method, &lt;init&gt;<br></br>a private method of this<br></br>a method in a superclass of this<br></br>The main use of invokespecial is to invoke an object's instance
initialization method, &lt;init&gt;, during the construction phase for a new
object. For example, when you write in Java:<br></br><pre>
new StringBuffer()
</pre>
code
like the following is generated:<br></br><pre>
new java/lang/StringBuffer         ; create a new StringBuffer
dup                                ; make an extra reference to the new instance
                                   ; now call an instance initialization method
invokespecial java/lang/StringBuffer/&lt;init&gt;()V
                                   ; stack now contains an initialized StringBuffer.
</pre>
invokespecial
is also used by the Java language by the 'super' keyword to access a
superclass's version of a method. For example, in the class:<br></br><pre>
   class Example {
       // override equals
       public boolean equals(Object x) {
            // call Object's version of equals
            return super.equals(x);
       }
   }
</pre>
the
'super.equals(x)' expression is compiled to:<br></br><pre>
aload_0  ; push 'this' onto the stack
aload_1  ; push the first argument (i.e. x) onto the stack
; now invoke Object's equals() method.
invokespecial java/lang/Object/equals(Ljava/lang/Object;)Z
</pre>
Finally,
invokespecial is used to invoke a private method. Remember that private methods
are only visible to other methods belonging the same class as the private
method.   <br></br>Before performing the method invokation, the class and the method identified by
&lt;method-spec&gt; are resolved. See Chapter 9 for a description of how
methods are resolved.<br></br>invokespecial first looks at the <i xmlns="">descriptor</i> given in
&lt;method-spec&gt;, and determines how many argument words the method takes
(this may be zero). It pops these arguments off the operand stack. Next it pops
<i xmlns="">objectref</i> (a reference to an object) off the operand stack.
<i xmlns="">objectref</i> must be an instance of the class named in &lt;method-spec&gt;,
or one of its subclasses. The interpreter searches the list of methods defined
by the class named in &lt;method-spec&gt;, looking for a method called
<i xmlns="">methodname</i> whose descriptor is <i xmlns="">descriptor</i>. This search is not
based on the runtime type of <i xmlns="">objectref</i>, but on the compile time type
given in &lt;method-spec&gt;.<br></br>Once a method has been located, invokespecial calls the method. First,
if the method is marked as synchronized, the monitor associated with
<i xmlns="">objectref</i> is entered. Next, a new stack frame structure is established
on the call stack. Then the arguments for the method (which were popped off the
current method's operand stack) are placed in local variables of the new stack
frame structure. arg1 is stored in local variable 1, arg2 is stored in local
variable 2 and so on. <i xmlns="">objectref</i> is stored in local variable 0 (the local
variable used for the special Java variable this). Finally, execution
continues at the first instruction in the bytecode of the new method. <br></br>Methods marked as native are handled slightly differently. For native
methods, the runtime system locates the platform-specific code for the method,
loading it and linking it into the JVM if necessary. Then the native method
code is executed with the arguments popped from the operand stack. The exact
mechanism used to invoke native methods is implementation-specific.<br></br>When the method called by invokespecial returns, any single (or
double) word return result is placed on the operand stack of the
current method. If the invoked method was marked as synchronized, the
monitor associated with <i xmlns="">objectref</i> is exited. Execution continues at the
instruction that follows invokespecial in the bytecode.</dd><dt>Exceptions</dt><dd>NullPointerException - <i xmlns="">objectref</i> is null<br></br>StackOverflowError - no more space in callstack for a new stack frame</dd><dt>Notes</dt><dd>1. In Java Virtual Machine implementations prior to version JDK 1.02,
this instruction was called invokenonvirtual, and was less restrictive
than invokespecial - it wasn't limited to invoking only superclass,
private or &lt;init&gt; methods. The class access flag ACC_SUPER (see Chapter
4) is used to indicate which semantics are used by a class. In older class
files, the ACC_SUPER flag is unset. In all new classes, the ACC_SUPER flag
should be set, indicating that the restrictions enforced by
invokespecial are obeyed. (In practice, all the common uses of
invokenonvirtual continue to be supported by invokespecial,
so this change should have little impact on JVM users).</dd><dt>See also</dt><dd><a href="ref-invokevirtual.html">invokevirtual</a> <a href="ref-.html"></a> <a href="ref-invokeinterface.html">invokeinterface</a> <a href="ref-.html"></a> <a href="ref-invokestatic.html">invokestatic</a> </dd><dt>Stack</dt><dd><table xmlns="">
<tr>
<td>
<b>Before </b>
</td>
<td><b>After</b></td></tr>
<tr>
<td>argN</td>
<td>[result]</td></tr>
<tr>
<td>---</td>
<td>...</td></tr>
<tr>
<td>arg2</td>
<td>...</td></tr>
<tr>
<td>arg1</td>
<td>...</td></tr>
<tr>
<td>objectref</td>
<td>...</td></tr>
<tr>
<td>...</td>
<td>...</td></tr>
</table></dd><dt>Bytecode</dt><dd><table xmlns="">
<tr>
<td>
<b> Type </b>
</td>
<td><b>Description</b></td></tr>
<tr>
<td>
u1 
</td>
<td>invokespecial
opcode = 0xB7 (183)
</td></tr>
<tr>
<td>
u2 
</td>
<td>index</td></tr>
</table></dd></dl></body></html>